﻿using DDit.Component.Tools.weixin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DDit.Core.Data.Entity.CoreEntity
{
    public class FansUser : BaseEntity
    {
        /// <summary>
        /// 用户是否订阅该公众号标识，值为0时，代表此用户没有关注该公众号，拉取不到其余信息。
        /// </summary>
        public int subscribe { get; set; }
        /// <summary>
        /// 用户的标识，对当前公众号唯一
        /// </summary>
        public string openid { get; set; }
        /// <summary>
        /// 用户的昵称
        /// </summary>
        public string nickname { get; set; }
        /// <summary>
        /// 用户的性别，值为1时是男性，值为2时是女性，值为0时是未知
        /// </summary>
        public string sex { get; set; }
        /// <summary>
        /// 用户头像，最后一个数值代表正方形头像大小
        /// （有0、46、64、96、132数值可选，0代表640*640正方形头像），
        /// 用户没有头像时该项为空。若用户更换头像，原有头像URL将失效
        /// </summary>
        public string headimgurl { get; set; }

        /// <summary>
        /// 公众号运营者对粉丝的备注，公众号运营者可在微信公众平台用户管理界面对粉丝添加备注
        /// </summary>
        public string remark { get; set; }
        /// <summary>
        /// 居住地
        /// </summary>
        public string Address { get; set; }

        public string Group { get; set; }
        /// <summary>
        /// 所在组
        /// </summary>        
        public ICollection<WxGroup> UserGroup { set; get; }
        
        public int Groupid { set; get; }
        public static FansUser Convert(WxUser item)
        {
            FansUser newitem = new FansUser();
            newitem.openid = item.openid;
            newitem.nickname = item.nickname;
            newitem.headimgurl = item.headimgurl;
            newitem.remark = item.remark;
            switch (item.sex)
            {
                case 1:
                    newitem.sex = "男";
                    break;
                case 2:
                    newitem.sex = "女";
                    break;
                default:
                    newitem.sex = "未知";
                    break;
            }

            newitem.Address = string.Format("{0} {1}  {2}", item.country, item.province, item.city);
            newitem.UserGroup = new List<WxGroup>();
            StringBuilder str = new StringBuilder();
            if (item.tagid_list != null)
            {
                var arr = JsonConvert.DeserializeObject<JArray>(item.tagid_list.ToString());
                var groups = WxUserManage.GetGroup(false);
                foreach (var i in arr)
                {
                    int val = i.Value<int>();
                    var find = groups.Where(p => p.id == val).FirstOrDefault();
                    if (find != null)
                    {
                        str.AppendLine(find.name);

                        newitem.UserGroup.Add(find);
                    }
                }
            }
            newitem.Group = str.ToString();
            return newitem;
        }
    }
}
